/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample.utils;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.InputSource;

/**
 *
 * @author Tu
 */
public class FoUtils {

    public static ByteArrayOutputStream transformToPdf(String webpath,Source source, String xslContainFoPath) {
        File fXsl = new File(xslContainFoPath);
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        if (source!=null && fXsl.exists()) {
            try {
                TransformerFactory tf = TransformerFactory.newInstance();
                StreamSource xsl = new StreamSource(fXsl);
                Transformer trans = tf.newTransformer(xsl);
                StringWriter sw = new StringWriter();
                StreamResult sr = new StreamResult(sw);
                
                trans.transform(source, sr);
                
                FopFactory foFactory= FopFactory.newInstance();
                FOUserAgent foUserAgent=foFactory.newFOUserAgent();
                File config=new File(webpath+"WEB-INF/FO/FOconfig.xconf");
                foFactory.setUserConfig(config);
                
                Fop fop=foFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
                TransformerFactory tfac= TransformerFactory.newInstance();
                Transformer transformer=tfac.newTransformer();
                
                //parse string to domsource, string is .fo content generated

                StreamSource xmlStringStream = new StreamSource(new StringReader(sw.toString()));
               // Source src=new DOMSource(XMLUtils.parseDOMFromString(sw.toString()));
                
                Result res=new SAXResult(fop.getDefaultHandler());

                transformer.transform(xmlStringStream, res);
                out.close();
                return out;
            } catch (TransformerConfigurationException ex) {
                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TransformerException ex) {
                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
            }catch(Exception ex){
                 Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return out;
    }

     public static ByteArrayOutputStream transformProductToPdf(String webpath,Source source, String xslContainFoPath) {
        File fXsl = new File(xslContainFoPath);
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        if (source!=null && fXsl.exists()) {
            try {
                 TransformerFactory tf = TransformerFactory.newInstance();
                StreamSource xsl = new StreamSource(fXsl);
                Transformer trans = tf.newTransformer(xsl);
                StringWriter sw = new StringWriter();
                StreamResult sr = new StreamResult(sw);

                trans.transform(source, sr);

                FopFactory foFactory= FopFactory.newInstance();
                FOUserAgent foUserAgent=foFactory.newFOUserAgent();
                File config=new File(webpath+"WEB-INF/FO/FOconfig.xconf");
                foFactory.setUserConfig(config);

                Fop fop=foFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
                TransformerFactory tfac= TransformerFactory.newInstance();
                Transformer transformer=tfac.newTransformer(new StreamSource(fXsl));

                //parse string to domsource, string is .fo content generated
                Result res=new SAXResult(fop.getDefaultHandler());
                transformer.transform(source, res);
                out.close();
                return out;
            } catch (TransformerConfigurationException ex) {
                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TransformerException ex) {
                Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
            }catch(Exception ex){
                 Logger.getLogger(FoUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return out;
    }
}
